package com.o2o.app.userCenter;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.o2o.app.R;
import com.o2o.app.userCenter.MineOrdDetailsActivity.ClickEvent;

public class MineHelpItemActivity extends Activity {
	/**
	 * 已发布内容
	 */
	private LinearLayout layout;

	/**
	 * 返回
	 */
	private Button btn_back;

	// 赞按钮
	private RelativeLayout rlt_demo;

	/**
	 * 赞开关 默认为true 可以点赞 false 不能点赞
	 */
	private Boolean b_zan = true;

	/**
	 * 点击赞布局动画
	 */
	private LinearLayout ll_Popup;
	// 加载动画
	private Animation animation1;

	/**
	 * 赞图片
	 */
	private ImageView iv_ding;

	/**
	 * 赞数量
	 */
	private TextView tv_dingnum;

	/**
	 * 定时器
	 */
	private Timer timer;
	private int time = 0;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				if ((Integer) msg.obj == 0) {
					timer.cancel();
					ll_Popup.setVisibility(View.GONE);
				}
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mine_help_item);

		initViews();
		showView();
	}

	private void initViews() {
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new ClickEvent());

		rlt_demo = (RelativeLayout) findViewById(R.id.rlt_demo);
		rlt_demo.setOnClickListener(new ClickEvent());

		iv_ding = (ImageView) findViewById(R.id.iv_ding);

		tv_dingnum = (TextView) findViewById(R.id.tv_dingnum);
		
		ll_Popup = (LinearLayout) findViewById(R.id.ll_popupLayout);
		animation1 = AnimationUtils.loadAnimation(this,
				R.anim.score_business_query_enter);
	}

	/**
	 * 点击事件
	 * 
	 * @author Administrator
	 * 
	 */
	class ClickEvent implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_back: { // 返回
				finish();
			}
				break;
			case R.id.rlt_demo: {
				if (b_zan) {
					ll_Popup.setVisibility(View.VISIBLE); // 显示布局
					ll_Popup.startAnimation(animation1); // 开始动画

					iv_ding.setBackgroundResource(R.drawable.ding_2);
					tv_dingnum.setTextColor(getResources()
							.getColor(R.color.red));

					b_zan = false;
					runTimerTask();
				}
			}
				break;
			default:
				break;
			}
		}

	}

	/**
	 * 显示已布局
	 */
	private void showView() {
		layout = (LinearLayout) findViewById(R.id.lly_all_content01);

		for (int i = 0; i < 2; i++) {
			LinearLayout child = (LinearLayout) LayoutInflater.from(
					MineHelpItemActivity.this).inflate(
					R.layout.mine_reply_item, null);
			child.setId(i);

			RelativeLayout rlt_item = (RelativeLayout) child
					.findViewById(R.id.rlt_item);
			// rlt_item.setId(CLICK_RLT);
			// rlt_item.setOnClickListener(new ClickEvent());
			// rltLists.add(rlt_item);

			layout.addView(child);
		}
	}

	/**
	 * 执行定时任务
	 */
	private void runTimerTask() {
		timer = new Timer();
		time = 1;
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 1;
				msg.obj = time;
				handler.sendMessage(msg);
				time--;
			}
		};
		timer.schedule(task, 0, 1000);
	}
}
